<?php
class Users extends MY_Controller {
        
    function __construct() {
        parent::__construct();
    }
 
    /**
     * 
     */
    public function index($id = NULL) {
        if ($this->session->userdata('user_id')) {
            $user = new User();
            $user->where('id', $this->session->userdata('user_id'))->get();
            echo 'Hello ' . $user->username;
        } else {
            echo 'Please login';
        }     
    }
    
    /*
     * Should be moved out of controller to be accessible from other classes
     * 
    public function authorize() {
        include 'core/libraries/doctrine/bootstrap.php';
        if (isset($_SESSION['user_id']) && ($_SESSION['user_id'] != '')) {
            $user = $doctrineEntityManager->find('User', $_SESSION['user_id']);
            if ($user) {
                return $user;
            }
        }
        return false;
    }
    
    public function isAdministrator() {
        $user = $this->authorize();
        return ($user && $user->role == 'administrator');
    }*/
    
    /**
     * 
     */
    public function get($id = NULL) {
        $user = new User();
        $user->where('id', $id)->get();
        if ($user->exists()) {
            echo $user->to_json();
        } else {
            echo 'User does not exist';
        }
    }
    
    /**
     * 
     */
    public function create() {
        $user = new User();
        if (isset($_GET['username']) && ($_GET['username'] != '')) {
            $user->username = $_GET['username'];
        }
        if (isset($_GET['password']) && ($_GET['password'] != '')) {
            $user->setPassword($_GET['password']);
        }
        if (isset($_GET['email']) && ($_GET['email'] != '')) {
            $user->email = $_GET['email'];
        }
        $user->activated = 1;
        $user->banned = 0;
        $user->last_ip = $_SERVER['REMOTE_ADDR'];
        $user->last_login = '0000-00-00 00:00:00';
        $user->created = date('Y-m-d H:i:s');
        $user->modified = '';
        if (isset($_GET['role']) && ($_GET['role'] != '')) {
            $user->role = $_GET['role'];
        }
        $user->save();
    }
    
    

    /**
     * 
     */
    public function update($id = NULL) {
        $user->where('id', $id)->get();
        if ($user->exists()) {
            if (isset($_GET['username']) && ($_GET['username'] != '')) { 
                $user->username = $_GET['username'];
            }
            if (isset($_GET['email']) && ($_GET['email'] != '')) { 
                $user->email = $_GET['email'];
            }
            if (isset($_GET['role']) && ($_GET['role'] != '')) { 
                $user->role = $_GET['role'];
            }
            $user->save();
        } else {
            echo 'User does not exist';
        }
    }

    /**
     * 
     */
    public function delete($id = NULL) {
        $user = new User();
        $user->where('id', $id)->get();
        if ($user->exists()) {
            $user->delete();
        } else {
            echo 'User does not exist';
        }
    }
    
    /**
     * 
     */
    public function addCourse(){
        if (isset($_GET['userId']) && ($_GET['userId']!= '') && isset($_GET['courseId']) && ($_GET['courseId']!= '')) {
            $user = new User();
            $course = new Course();
            $userId = $_GET['userId'];
            $courseID = $_GET['courseId'];
            $course = $course->where('id', $courseID)->get();
            $user = $user->where('id', $userId)->get();
            
            if ($course->exists() && $user->exists()) {
                $user->save($course);
            } else {
                echo 'User or Course does not exist';
            }
        } else {
            return;
        }
    }
    
    /**
     * 
     */
    public function removeCourse(){
        if (isset($_GET['userId']) && ($_GET['userId']!= '') && isset($_GET['courseId']) && ($_GET['courseId']!= '')) {
            $user = new User();
            $course = new Course();
            $userId = $_GET['userId'];
            $courseID = $_GET['courseId'];
            $course = $course->where('id', $courseID)->get();
            $user = $user->where('id', $userId)->get();
            
            if ($course->exists() && $user->exists()) {
                $user->delete($course);
            } else {
                echo 'User or Course does not exist';
            }
        } else {
            return;
        }
    }
    
    /**
     * 
     */
     public function login() {
         $this->load->library('ion_auth');
         	$identity = $_GET['identity'];
		$password = $_GET['password'];
		$remember = TRUE; // remember the user
		$this->ion_auth->login($identity, $password, $remember);
         
         
         
      /*  if ( !isset($_SESSION['user_id']) || ($_SESSION['user_id'] == '')) {
            if (isset($_GET['email']) && ($_GET['email']!= '')) {
                $user = new User();
                $email = $_GET['email'];                
                $user = $user->where('email', $email)->get();
                if ($user->exists()) {
                    if (isset($_GET['password']) && ($_GET['password'] != '')) {
                        $password = $_GET['password'];
                        if (md5($password) == $user->password) {
                            $data = array(
                                        'user_id'  => $user->id
                                    );
                            $this->session->set_userdata($data);
                            /*if (isset($_GET['rememberme']) && ($_GET['rememberme']!= '')) {
                                setcookie('user_id', $user->getId(), time()+3600,"/", "institute.gr", 1);
                            }
                            
                            $user->last_ip = $_SERVER['REMOTE_ADDR'];
                            $user->last_login = date('Y-m-d H:i:s');
                            if ($user->save()) {
                                header('Location: index');
                            }
                        } else {
                            echo 'Wrong Password';
                        }
                    } else {
                        echo 'Missing Password';
                    }
                } else {
                    echo 'User with email ' . $email .' does not exist';    
                }
            } else {
                echo 'Email required';
            }
        } else { // user is already logged in
            header('Location: index');
        }
    }*/
     } 
    public function logout() {
        $this->session->unset_userdata('user_id');
        header("location: index");
    }
}

?>